// pages/articlesList/articlesList.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
  data: {
    isChoose: 0,
    page: 0,
    pageSize: 5,
    totalDataCount: 0,
    articleList: [],
    tags: null
  },

	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    this.getList(0);
    // this.setData({
    // 	articleList: app.globalData.articleList
    // })
    // console.log(this.data.articleList)

    // let list = this.data.articleList;
    // debugger
    // let tags = ["全部"];
    // if (list) {
    //   let tagsArr = [];
    //   for (let i = 0; i < list.length; i++) {
    //     list[list.length - 1].tags = ['vue.js'];
    //     tagsArr = tagsArr.concat(list[i].tags);
    //   }
    //   this.setData({
    //     tags: tags.concat(Array.from(new Set(tagsArr)))
    //   })
    // }

    // console.log(this.data.tags)

  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },
  getList: function (option) {
    // 使用 Mock
    let data = {};
    let that = this;

    if (option == 0) {

      app.globalData.messageList = [];
      that.setData({
        articleList: [],
        page: 0
      })
    }
    let start = that.data.page * that.data.pageSize;
    let curPage = that.data.page + 1;
    data.ApiCode = "b81ad11aa499429dbd518aa4631fba2a";
    data.requestDev = "小程序博客";
    data.param = "{'start':'" + start + "','type':'" + 7 + "'}";


    wx.request({
      url: 'https://qxsdcloud.com/data/getMessageRow',
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      data: data,
      method: 'post',
      success: function (e) {
        let jsonStr = JSON.parse(e.data.rows)
        let list = app.globalData.messageList.concat(jsonStr);
        app.globalData.messageList = list;
        if (jsonStr.length > 0) {
          let tags = ["全部"];
          let tagsArr = [];
          for (let i = 0; i < list.length; i++) {
            tagsArr = tagsArr.concat(list[i].tags);
          }
          that.setData({
            articleList: list,
            page: curPage,
            tags: tags.concat(Array.from(new Set(tagsArr)))
          })
        } else {
          wx.showToast({
            title: '到底啦O(∩_∩)O',
            icon: 'success',
            duration: 2000
          })

        }



      },
      fail: function (e) {
        debugger
      }
    });

  },
  // 页面上拉触底事件的处理函数 
  onReachBottom: function () {
    this.getList(1)
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 1000//持续的时间

    })

  },
	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {

  },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },


	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  },
	/**
	 * 文章详情
	 */
  showArticle: function (e) {
    // console.log(e)
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/articleContent/articleContent?id=' + e.currentTarget.dataset.id+'&type=message',
    })
  },
	/**
	 * 选择标签
	 */
  chooseTag: function (e) {
    // console.log(e)
    let tag = e.currentTarget.dataset.tag;
    let tagIndex = e.currentTarget.dataset.index;
    this.setData({
      isChoose: tagIndex
    })
    if (tag == '全部') {
      this.setData({
        articleList: app.globalData.messageList
      })
    } else {
      let list = app.globalData.messageList;
      let tagArticle = list.filter((item, index) => {
        return item.tags.indexOf(tag) != -1
      })
      this.setData({
        articleList: tagArticle
      })
    }
    // console.log(this.data.articleList)

  }
})